import {Group, Tween,Easing,Interpolation } from "@tweenjs/tween.js";
import JNFrameTweenSystem from "./JNFrameTweenSystem";
import { Node } from "cc";

export default class JNFrameTween<T> extends Tween<T> {

    private node:Node;

    //是否Node可用
    isNodeValid():boolean {
        if(!this.node) return true;
        return this.node.isValid;
    }

    constructor(_object: T,node?:Node, _group?: false | Group){
        super(_object,_group);
        this.node = node;
    }

    start(time?: number, overrideStartingValues?: boolean): this {

        if(!this.isNodeValid) return this;

        time = JNFrameTweenSystem.getInstance().addTime;
        JNFrameTweenSystem.getInstance().addTween(this);
        super.start(time, overrideStartingValues);
        return this;
    }

    update(time?: number, autoStart?: boolean): boolean {
        let bool = super.update(time, autoStart);
        if((!bool && JNFrameTweenSystem.getInstance().hasTween(this)) || !this.isNodeValid){
            JNFrameTweenSystem.getInstance().delTween(this);
        }
        return bool;
    }

    stop(): this {
        super.stop();
        if(JNFrameTweenSystem.getInstance().hasTween(this)){
            JNFrameTweenSystem.getInstance().delTween(this);
        }
        return this;
    }

}

export function JTween<T>(data:T,node?:Node):JNFrameTween<T>{
    return new JNFrameTween<T>(data,node);
}

export const JEasing = Easing;
